// 请求超时处理
const timeoutHandler = (timeout = 10000) => {
    let timeID = setTimeout(() => {
        if (requestControl) {
            requestControl.abort();
        }
        uni.hideLoading();
        uni.showToast({
            title: "请求超时",
            icon: "error",
            mask: true,
        });
    }, timeout);

    let requestControl;

    return {
        clearTimeout() {
            clearTimeout(timeID);
        },
        receiveRequestControl(request) {
            requestControl = request;
        },
    };
};

// 封装微信网络请求

// 可以在这里设置请求头
let baseHeader = {};
// 可以在这里配置公共参数
let baseData = {};

interface Others {
    data?: {
        [name: string]: String;
    };
    headers?: {
        [name: string]: String;
    };
}
export const all = function (url: String, method = "GET", othors?: Others) {
    try {
        var { data, headers } = othors as Others;
    } catch {}
    method = method.toUpperCase();

    const { clearTimeout, receiveRequestControl } = timeoutHandler();
    return new Promise((resolve, reject) => {
        receiveRequestControl(
            uni.request({
                url,
                method,
                data: {
                    ...baseData,
                    ...data,
                },
                header: {
                    ...baseHeader,
                    ...headers,
                },
                success({ data }) {
                    clearTimeout();
                    resolve(data);
                },
                fail(err) {
                    clearTimeout();
                    reject(err);
                },
            })
        );
    });
};

export const get = function (url: String, othors?: Others) {
    try {
        var { data, headers } = othors as Others;
    } catch {}
    const { clearTimeout, receiveRequestControl } = timeoutHandler();
    return new Promise((resolve, reject) => {
        receiveRequestControl(
            uni.request({
                url,
                method: "GET",
                data: {
                    ...baseData,
                    ...data,
                },
                header: {
                    ...baseHeader,
                    ...headers,
                },
                success({ data }) {
                    clearTimeout();
                    resolve(data);
                },
                fail(err) {
                    clearTimeout();
                    reject(err);
                },
            })
        );
    });
};
export const post = function (url: String, othors?: Others) {
    try {
        var { data, headers } = othors as Others;
    } catch {}
    const { clearTimeout, receiveRequestControl } = timeoutHandler();
    return new Promise((resolve, reject) => {
        receiveRequestControl(
            uni.request({
                url,
                method: "POST",
                data: {
                    ...baseData,
                    ...data,
                },
                header: {
                    ...baseHeader,
                    ...headers,
                },
                success(res) {
                    clearTimeout();
                    // console.log("$post res =>", res);
                    resolve(res.data);
                },
                fail(err) {
                    clearTimeout();
                    reject(err);
                },
            })
        );
    });
};

export const requestMedia = function (url: String, othors?: Others) {
    try {
        var { data, headers } = othors as Others;
    } catch {}
    const { clearTimeout, receiveRequestControl } = timeoutHandler();
    return new Promise((resolve, reject) => {
        receiveRequestControl(
            uni.request({
                url,
                method: "GET",
                data: {
                    ...baseData,
                    ...data,
                },
                responseType: "arraybuffer",
                header: {
                    ...baseHeader,
                    ...headers,
                },
                success({ data }) {
                    clearTimeout();
                    let base64 = uni.arrayBufferToBase64(data);
                    resolve(base64);
                },
                fail(err) {
                    clearTimeout();
                    reject(err);
                },
            })
        );
    });
};
